import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { AutoInspectionItemModel } from '#/api/equipment/models';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'subjectName',
      label: '点检项名称',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Input',
      fieldName: 'symbol',
      label: '点位',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'InputNumber',
      fieldName: 'minLimit',
      label: '下限值',
    },
    {
      component: 'InputNumber',
      fieldName: 'maxLimit',
      label: '上限值',
    },
    {
      component: 'Input',
      fieldName: 'measurementUnit',
      label: '测量单位',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '否', value: false },
          { label: '是', value: true },
        ],
      },
      fieldName: 'isCheckThreshold',
      label: '校验阈值',
      defaultValue: false,
      help: '设置后将由系统来判定结果，会将采集数据与设定的上下限进行对比，得出单项判定结果以及最终判定结果',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '1', value: '1' },
          { label: '0.1', value: '0.1' },
          { label: '0.01', value: '0.01' },
          { label: '0.001', value: '0.001' },
          { label: '10', value: '10' },
          { label: '100', value: '100' },
          { label: '1000', value: '1000' },
        ],
      },
      fieldName: 'conversion',
      label: '倍率',
      help: '对原始的数据进行转换处理',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '0', value: 0 },
          { label: '1', value: 1 },
          { label: '2', value: 2 },
          { label: '3', value: 3 },
          { label: '4', value: 4 },
          { label: '5', value: 5 },
        ],
      },
      fieldName: 'digits',
      label: '保留小数位',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<AutoInspectionItemModel>,
): VxeTableGridOptions<AutoInspectionItemModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'subjectName', title: '点检项名称', minWidth: 180 },
    { field: 'symbol', title: '点位', width: 200 },
    { field: 'minLimit', title: '下限值', width: 100 },
    { field: 'maxLimit', title: '上限值', width: 100 },
    { field: 'measurementUnit', title: '测量单位', width: 100 },
    {
      field: 'isCheckThreshold',
      title: '阈值校验',
      width: 80,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'blue', label: '是', value: true },
          { color: 'orange', label: '否', value: false },
        ],
      },
    },
    { field: 'conversion', title: '倍率', width: 100 },
    { field: 'digits', title: '小数位', width: 100 },
    {
      cellRender: {
        attrs: {
          nameField: 'subjectName',
          nameTitle: '点检项',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
